package com.chenbei.system.aspectj;

import com.chenbei.common.enums.ResponseResultEnum;
import com.chenbei.common.util.IPUtil;
import com.chenbei.common.domain.vo.AdminUserLogin;
import com.chenbei.common.domain.vo.IPRegionInfo;
import com.chenbei.common.domain.vo.ApiResponse;
import com.chenbei.system.domain.SysLoginLog;
import com.chenbei.system.service.ISysLoginLogService;
import eu.bitwalker.useragentutils.UserAgent;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.time.LocalDateTime;

@Aspect
@Component
@Slf4j
public class LoginAspectj {

    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private ISysLoginLogService loginLogService;

    @Autowired
    private ApplicationEventPublisher publisher;

    @Autowired
    private DataSource dataSource;

    @Pointcut("@annotation(com.chenbei.common.annotation.LoginLog)")
    public void loginLog(){}

    @Around("loginLog()")
    @SuppressWarnings("unchecked")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        AdminUserLogin adminUserLogin = (AdminUserLogin)args[0];
        //执行目标方法
        Object proceed = pjp.proceed();
        ApiResponse<Object> apiResponse = (ApiResponse<Object>) proceed;
        saveLoginLog(adminUserLogin, apiResponse);
        return proceed;
    }

    @Async("DefaultTaskExecutor")
    public void saveLoginLog(AdminUserLogin adminUserLogin, ApiResponse<Object> apiResponse) {
        SysLoginLog sysLoginLog = new SysLoginLog();
        sysLoginLog.setLoginName(adminUserLogin.getLoginName());
        sysLoginLog.setLoginIp(IPUtil.getIpAddr(httpServletRequest));
        if (StringUtils.isNoneBlank(sysLoginLog.getLoginIp())) {
            IPRegionInfo regionByIP = IPUtil.getRegionByIP(sysLoginLog.getLoginIp());
            if (regionByIP != null) {
                sysLoginLog.setLoginRegion(regionByIP.getProvince() + regionByIP.getCity());
            }
        }

        String userAgent = httpServletRequest.getHeader("User-Agent");
        UserAgent userAgent1 = UserAgent.parseUserAgentString(userAgent);
        sysLoginLog.setAgentClient(userAgent1.getBrowser().getName());
        sysLoginLog.setOperatingSystem(userAgent1.getOperatingSystem().getName());
        sysLoginLog.setCreateTime(LocalDateTime.now());

        if (apiResponse.getCode() == ResponseResultEnum.SUCCESS.getCode()) {
            sysLoginLog.setStatus(1);
            sysLoginLog.setMessage("登录成功");
        } else {
            sysLoginLog.setStatus(2);
            sysLoginLog.setMessage(apiResponse.getMessage());
        }
        loginLogService.save(sysLoginLog);
    }
}
